#include "SdkMic.h"

#define DEF_TAG             "Mic"

int g_ChipSdk_ai_devID = 1;
int g_ChipSdk_ai_chnID = 0;
IMPAudioFrame g_ChipSdk_ai_frm;

int Ext_SdkMic_Init(void)
{
    int ret;
    /* Step 1: set public attribute of AI device. */
    IMPAudioIOAttr attr;
    attr.samplerate = AUDIO_SAMPLE_RATE_8000;
    attr.bitwidth = AUDIO_BIT_WIDTH_16;
    attr.soundmode = AUDIO_SOUND_MODE_MONO;
    attr.frmNum = 40;
    attr.numPerFrm = 320;
    attr.chnCnt = 1;

    ret = IMP_AI_SetPubAttr(g_ChipSdk_ai_devID, &attr);
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "set ai %d attr err: %d\n", g_ChipSdk_ai_devID, ret);
        return -1;
    }

    memset(&attr, 0x0, sizeof(attr));
    ret = IMP_AI_GetPubAttr(g_ChipSdk_ai_devID, &attr);
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "get ai %d attr err: %d\n", g_ChipSdk_ai_devID, ret);
        return -1;
    }

    PRT_INFO(DEF_TAG, "Audio In GetPubAttr samplerate : %d\n", attr.samplerate);
    PRT_INFO(DEF_TAG, "Audio In GetPubAttr   bitwidth : %d\n", attr.bitwidth);
    PRT_INFO(DEF_TAG, "Audio In GetPubAttr  soundmode : %d\n", attr.soundmode);
    PRT_INFO(DEF_TAG, "Audio In GetPubAttr     frmNum : %d\n", attr.frmNum);
    PRT_INFO(DEF_TAG, "Audio In GetPubAttr  numPerFrm : %d\n", attr.numPerFrm);
    PRT_INFO(DEF_TAG, "Audio In GetPubAttr     chnCnt : %d\n", attr.chnCnt);

    /* Step 2: enable AI device. */
    ret = IMP_AI_Enable(g_ChipSdk_ai_devID);
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "enable ai %d err\n", g_ChipSdk_ai_devID);
        return -1;
    }

    /* Step 3: set audio channel attribute of AI device. */
    IMPAudioIChnParam chnParam;
    chnParam.usrFrmDepth = 40;
    chnParam.aecChn = 0;
    ret = IMP_AI_SetChnParam(g_ChipSdk_ai_devID, g_ChipSdk_ai_chnID, &chnParam);
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "set ai %d channel %d attr err: %d\n", g_ChipSdk_ai_devID, g_ChipSdk_ai_chnID, ret);
        return -1;
    }

    memset(&chnParam, 0x0, sizeof(chnParam));
    ret = IMP_AI_GetChnParam(g_ChipSdk_ai_devID, g_ChipSdk_ai_chnID, &chnParam);
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "get ai %d channel %d attr err: %d\n", g_ChipSdk_ai_devID, g_ChipSdk_ai_chnID, ret);
        return -1;
    }

    PRT_INFO(DEF_TAG, "Audio In GetChnParam usrFrmDepth : %d\n", chnParam.usrFrmDepth);

    /* Step 4: enable AI channel. */
    ret = IMP_AI_EnableChn(g_ChipSdk_ai_devID, g_ChipSdk_ai_chnID);
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio Record enable channel failed\n");
        return -1;
    }

    ret = IMP_AI_EnableAec(g_ChipSdk_ai_devID, g_ChipSdk_ai_chnID, 0, 0);
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio Record enable channel failed\n");
        return -1;
    }

    /*
    ret = IMP_AI_EnableHs();
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Enable Hs failed\n");
        return -1;
    }
    */

    /* Step 5: Set audio channel volume. */
    int chnVol = 60;
    ret = IMP_AI_SetVol(g_ChipSdk_ai_devID, g_ChipSdk_ai_chnID, chnVol);
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio Record set volume failed\n");
        return -1;
    }

    ret = IMP_AI_GetVol(g_ChipSdk_ai_devID, g_ChipSdk_ai_chnID, &chnVol);
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio Record get volume failed\n");
        return -1;
    }

    PRT_INFO(DEF_TAG, "Audio In GetVol    vol : %d\n", chnVol);

    int aigain = 18;
    ret = IMP_AI_SetGain(g_ChipSdk_ai_devID, g_ChipSdk_ai_chnID, aigain);
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio Record Set Gain failed\n");
        return -1;
    }

    ret = IMP_AI_GetGain(g_ChipSdk_ai_devID, g_ChipSdk_ai_chnID, &aigain);
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio Record Get Gain failed\n");
        return -1;
    }

    PRT_INFO(DEF_TAG, "Audio In GetGain    gain : %d\n", aigain);

    return 0;
}

int Ext_SdkMic_Exit(void)
{
    int ret;

    /*
    ret = IMP_AI_DisableHs();
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Disable Hs failed\n");
        return -1;
    }
    */

    ret = IMP_AI_DisableAec(g_ChipSdk_ai_devID, g_ChipSdk_ai_chnID);
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "IMP_AI_DisableAecRefFrame\n");
        return -1;
    }
    sleep(3);

    /* Step 11: disable the audio channel. */
    ret = IMP_AI_DisableChn(g_ChipSdk_ai_devID, g_ChipSdk_ai_chnID);
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio channel disable error\n");
        return -1;
    }

    /* Step 12: disable the audio devices. */
    ret = IMP_AI_Disable(g_ChipSdk_ai_devID);
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio device disable error\n");
        return -1;
    }

    return 0;
}

int Ext_SdkMediaCom_GetMicData(pStSdkAudioInfo info)
{
    int ret;

    ret = IMP_AI_PollingFrame(g_ChipSdk_ai_devID, g_ChipSdk_ai_chnID, 1000);
    if (ret != 0 )
    {
        IMP_LOG_ERR(DEF_TAG, "Audio Polling Frame Data error\n");
    }

    ret = IMP_AI_GetFrame(g_ChipSdk_ai_devID, g_ChipSdk_ai_chnID, &g_ChipSdk_ai_frm, BLOCK);
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio Get Frame Data error\n");
        return DEF_COM_FAILED;
    }

    info->size = g_ChipSdk_ai_frm.len;
    info->buf = (Com_pu8)g_ChipSdk_ai_frm.virAddr;
    info->pts = g_ChipSdk_ai_frm.timeStamp;

    return DEF_COM_SUCCESS;
}

void Ext_SdkMediaCom_ReleaseMicData(void)
{
    int ret;

    /* Step 9: release the audio record frame. */
    ret = IMP_AI_ReleaseFrame(g_ChipSdk_ai_devID, g_ChipSdk_ai_chnID, &g_ChipSdk_ai_frm);
    if(ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio release frame data error\n");
        return;
    }
}
